from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select


driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://shop.6360.cn/')
# 强制等待
sleep(2)
# 隐式等待
driver.implicitly_wait(10)
# 点击登录
driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div/div[2]/a').click()
sleep(2)
# 点击账密登录
driver.find_element(By.XPATH, '//*[@id="login-wrapper"]/div/div[1]/div/div/div/div[2]/a').click()
sleep(2)
# 输入手机号
driver.find_element(By.NAME, 'userName').send_keys('15985645480')
sleep(2)
# 输入密码
driver.find_element(By.NAME, 'password').send_keys('123456Aa')
sleep(2)
# 点击登录按钮
driver.find_element(By.CSS_SELECTOR, '#login-wrapper > div > div.quc-panel-bd > div > div > div > div.quc-login-content > form > div.quc-button-list > input').click()
sleep(10)

# B：成功立即购买商品

# 点击商品图片   显示等待
tp = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[2]/div/div[1]/a/div[1]/img'))
)
tp.click()
sleep(2)
# 点击立即购买
driver.find_element(By.XPATH, '//*[@id="js-picture-exchange"]').click()
sleep(2)
# 点击请填写收货地址
driver.find_element(By.XPATH, '//*[@id="js-address-btn"]').click()
sleep(10)
# 输入收件人姓名
driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[1]/div[2]/input').send_keys('小高先生')
sleep(2)
# 输入联系电话
driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[2]/div[2]/input').send_keys('15985645480')
sleep(2)
# 选择收货地址
# 省
select1 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[3]/div[2]/div/div[1]/label[1]/select'))
select1.select_by_value('北京市')
sleep(2)
# 市
select2 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[3]/div[2]/div/div[1]/label[2]/select'))
select2.select_by_index(1)
sleep(2)
# 区
select3 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[3]/div[2]/div/div[1]/label[3]/select'))
select3.select_by_visible_text('昌平区')
sleep(2)
# 输入收货地址
driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[3]/div[2]/div/div[2]/textarea').send_keys('南口南大街21号')
sleep(2)
# 点击保存并提交
driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div[4]/a').click()
sleep(3)
# 点击确认购买
driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div[2]/div/div/div[4]/div/div[1]/div[1]/div[2]/div[5]/div/button').click()
sleep(3)
# 点击取消支付
try:
    driver.find_element(By.XPATH, '/html/body/div/div[2]/div[3]/div[1]').click()
except Exception as e:
    print(f'异常:{e}')
    raise

sleep(10)




